Guide to NoSQL with Azure CosmosDB by Gaston C. Hillar
Author:Gaston C. Hillar [Gaston C. Hillar]
Language: eng
Format: epub
Tags: COM021000 - COMPUTERS / Databases / General, COM060080 - COMPUTERS / Web / General, COM021050 - COMPUTERS / Databases / Servers
Publisher: Packt
Published: 2018-09-28T06:35:51+00:00
Querying and creating document collections
The following lines declare the code for the CreateCollectionIfNotExistsAsync asynchronous static method, which creates a new document collection if a collection with id equal to the value stored in the collectionId field doesn't exist in the database. Add the following lines to the existing code of the Program.cs file. The code file for the sample is included in the learning_cosmos_db_04_01 folder in the dot_net_core_2_samples/SampleApp1/SampleApp1/Program.cs file:
private static async Task<DocumentCollection> CreateCollectionIfNotExistsAsync() { var databaseUri = UriFactory.CreateDatabaseUri(databaseId); DocumentCollection documentCollectionResource; var isCollectionCreated = await client.CreateDocumentCollectionQuery(databaseUri) .Where(c => c.Id == collectionId) .CountAsync() == 1; if (isCollectionCreated) { Console.WriteLine($"The collection {collectionId} already exists."); var documentCollectionUri = UriFactory.CreateDocumentCollectionUri(databaseId, collectionId); var documentCollectionResponse = await client.ReadDocumentCollectionAsync(documentCollectionUri); documentCollectionResource = documentCollectionResponse.Resource; } else { var documentCollection = new DocumentCollection { Id = collectionId, }; documentCollection.PartitionKey.Paths.Add("/location/zipCode"); var uniqueKey = new UniqueKey(); uniqueKey.Paths.Add("/title"); documentCollection.UniqueKeyPolicy.UniqueKeys.Add(uniqueKey); var requestOptions = new RequestOptions { OfferThroughput = 1000, }; var collectionResponse = await client.CreateDocumentCollectionAsync( databaseUri, documentCollection, requestOptions); if (collectionResponse.StatusCode == System.Net.HttpStatusCode.Created) { Console.WriteLine($"The collection {collectionId} has been created."); } documentCollectionResource = collectionResponse.Resource; } return documentCollectionResource; }
The code doesn't use the easiest mechanism to create a collection when it doesn't exist because we will analyze how we can query the document collections for a database. Our goal is to learn about many possibilities offered by the SDK that will enable us to develop many different kinds of applications that work with Cosmos DB. However, it is very important to notice that the code for this method could be simplified by calling the CreateDocumentCollectionIfNotExistsAsync method.
First, the code calls the UriFactory.CreateDatabaseUri method with databaseId as its argument and saves the result in the databaseUri variable. This method will return a Uri instance with the URI for the database ID received as an argument. The code will use this URI to easily address the database resource in which we have to perform operations.
Note that the UriFactory.CreateDatabaseUri method requires the database ID to build the Uri instance and doesn't require any query to the database. However, of course, we must be sure that the database resource with the specified ID already exists before using the generated URI.
The next line declares the documentCollectionResource variable as a DocumentCollection instance. The code will end up returning this variable.
Then, the code creates a LINQ query with a call to the asynchronous client.CreateDocumentCollectionQuery method with databaseUri as an argument. This counts the number of collections whose Id is equal to collectionId with an asynchronous execution due to the usage of the chained CountAsync method. If the results of this query on the collections for the database is 1, it means that the collection already exists.
The following lines show the code that builds the LINQ query and stores the results of the Boolean expression in the isCollectionCreated variable:
var isCollectionCreated = await client.CreateDocumentCollectionQuery(databaseUri) .Where(c => c.Id == collectionId) .CountAsync() == 1;
We can easily query the existing collections in a document database by chaining LINQ expressions to the call to the CreateDocumentCollectionQuery method. In this case, we are always working with asynchronous methods. If we used the Count method instead of CountAsync, the query would have a synchronous execution.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8300)
Azure Data and AI Architect Handbook by Olivier Mertens & Breght Van Baelen(6743)
Building Statistical Models in Python by Huy Hoang Nguyen & Paul N Adams & Stuart J Miller(6720)
Serverless Machine Learning with Amazon Redshift ML by Debu Panda & Phil Bates & Bhanu Pittampally & Sumeet Joshi(6597)
Data Wrangling on AWS by Navnit Shukla | Sankar M | Sam Palani(6381)
Driving Data Quality with Data Contracts by Andrew Jones(6329)
Machine Learning Model Serving Patterns and Best Practices by Md Johirul Islam(6093)
Learning SQL by Alan Beaulieu(5995)
Weapons of Math Destruction by Cathy O'Neil(5779)
Big Data Analysis with Python by Ivan Marin(5365)
Data Engineering with dbt by Roberto Zagni(4363)
Solidity Programming Essentials by Ritesh Modi(4010)
Time Series Analysis with Python Cookbook by Tarek A. Atwan(3872)
Pandas Cookbook by Theodore Petrou(3579)
Blockchain Basics by Daniel Drescher(3294)
Hands-On Machine Learning for Algorithmic Trading by Stefan Jansen(2905)
Feature Store for Machine Learning by Jayanth Kumar M J(2814)
Learn T-SQL Querying by Pam Lahoud & Pedro Lopes(2796)
Mastering Python for Finance by Unknown(2744)
